/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.controller;

import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.mobile.service.MMessageService;
import com.cmcc.zysoft.infocollection.mobile.service.MNewsService;
import com.cmcc.zysoft.infocollection.mobile.service.MReadNewsService;
import com.cmcc.zysoft.infocollection.mobile.service.MSystemUserService;
import com.cmcc.zysoft.infocollection.model.News;
import com.cmcc.zysoft.infocollection.model.SystemUser;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MNewsController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-12 上午11:30:48
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/mobile/news")
public class MNewsController extends BaseController {

	/**
	 * 属性名称: mNewsService.
	 * 类       型: MNewsService.
	 */
	@Resource
	private MNewsService mNewsService;
	
	/**
	 * 属性名称: mReadNewsService.
	 * 类       型: MReadNewsService.
	 */
	@Resource
	private MReadNewsService mReadNewsService;
	
	/**
	 * 属性名称: mMessageService.
	 * 类       型: MMessageService.
	 */
	@Resource
	private MMessageService mMessageService;
	
	/**
	 * 属性名称: mSystemUserService.
	 * 类       型: MSystemUserService.
	 */
	@Resource
	private MSystemUserService mSystemUserService;
	
	/**
	 * 获取最新通知-通过该接口,显示用户未读的最新一条信息.
	 * @param usercode 手机号
	 * @return map
	 */
	@RequestMapping("/unread.htm")
	@ResponseBody
	public Map<String, Object> unread(String usercode) {
		return this.mNewsService.getUnreadNews(usercode);
	}
	
	/**
	 * 查看消息.
	 * @param type 类型:0-news,1-message
	 * @param messageId 消息ID
	 * @param usercode 手机号
	 * @return map
	 */
	@RequestMapping("/read.htm")
	public String read(ModelMap modelMap, String type, String messageId, String usercode) {
		SystemUser systemUser = null;
		if("0".equals(type)) {
			//设置通知的已读状态或者更新阅读次数
			this.mReadNewsService.read(messageId, usercode);
			News news = this.mNewsService.getEntity(messageId);
			systemUser = this.mSystemUserService.getEntity(news.getPublishMan());
			modelMap.addAttribute("news", news);
			modelMap.addAttribute("userName", systemUser.getRealName());
		} else {
			//设置消息的已读状态
			this.mMessageService.readMessage(messageId, usercode);
			Map<String, Object> map = this.mMessageService.getMessageById(messageId);
			systemUser = this.mSystemUserService.getEntity(map.get("addMan").toString());
			modelMap.addAttribute("news", map);
			modelMap.addAttribute("userName", systemUser.getRealName());
		}
		return "/mobile/news/detail";
	}
	
	/**
	 * 消息中心-通过该接口,用户可查看自己的未通过信息和省公司下发的通知公告.
	 * @param usercode 手机号
	 * @param page 页码
	 * @param pagesize 每页显示个数
	 * @return map
	 */
	@RequestMapping("/center.htm")
	@ResponseBody
	public Map<String, Object> center(String usercode, int page, int pagesize) {
		Date date1 = new Date();
		Map<String, Object> map = this.mNewsService.getNews(usercode, page, pagesize);
		Date date2 = new Date();
		System.out.println("==========================================================");
		System.out.println("消息中心接口   耗时   : " + (date2.getTime() - date1.getTime()) + "ms");
		System.out.println("==========================================================");
		return map;
	}
}